<?php
/**
 * Created by PhpStorm.
 * User: windon
 * Date: 2018/6/3
 * Time: 14:53
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class JpushMobile extends Model
{
    protected $table = 'wy_jpush_mobile';
    public $timestamps = false;
    protected $primaryKey = 'id';

    /*
     * 存储设备标识
     * @$params array
     * */
    public static function saveJpush($params)
    {
        if(!is_array($params) || !isset($params['mobile_unique_code']))
            return false;

        $model = self::where([ [ 'mobile_unique_code', '=', $params['mobile_unique_code'] ] ])->first();
        if(empty($model) ){
            $model = new JpushMobile();
            $model->mobile_unique_code = $params['mobile_unique_code'];
            $model->option_time = time();
            $model->registration_id = $params['registration_id'];
            if($model->save())
                return true;
        }
        if($model->registration_id == $params['registration_id'])
            return true;
        if(self::where(['id'=>$model->id])->update(["registration_id"=>$params['registration_id']]))
            return true;
        return false;
    }

}